<template>
	<view class="login-page">
		<view class="login-warp">
			<view class="logo-warp">
				<image class="logo" src="/static/image//Logo.png"></image>
			</view>
			<view class="logo-text">
				Hello， 
			</view>
			<view class="logo-text">
				欢迎使用合极智能监控系统
			</view>
			<form class="form-warp" @submit="formSubmit">
				<view class="account">
					账号
				</view>
				<view class="input-group">
					<input type="number" v-model="uid" name="uid" placeholder="请输入账号" />
				</view>
				<view class="textSize">
					密码
				</view>
				<view class="input-group">
					<input password v-model="upwd" name="upwd" placeholder="请输入密码" />
				</view>
				<!-- <view class="input-group">
					<label style="padding-left: 15px;" v-html="resText.lblLange"></label>
					<text style="padding-left: 10px;" class="lang" @click="ChangeLange">{{langText}}</text>
				</view> -->
			<!-- 	<view style="margin-top: 13px;">
					<label class="radioCheck"><radio value="true" :checked="isChecked" @click="radioClick"/>自动登录</label>
				</view> -->
				<button form-type="submit" class="uni-button">登录</button>
				<navigator class="btn-setting" url="../setting/index">服务器配置</navigator>
				<view class="registered">
					新用户注册
				</view>
			</form>
			<!-- <view class="version">Copyright © 2000-2022<text>合极.版权所有 ver{{subTitle}}</text></view> -->
		</view>
	</view>
</template>
<script>
	var that;
	var apiUrls;
	var apiPorts;
	
	import {
		mapMutations
	} from 'vuex';
	
	export default {
		onReady: async function() {
			that = this;
			this.checkUpdate().then(function(res) {
				if (res) return;
			});
		},
		data() {
			return {
				title: '',
				subTitle: '',
				uid: '', // 用户id
				upwd:'', // 用户密码
				isChecked: false, // 是否自动登录
			}
		},
		onLoad() {
			that = this;
			
			let {
				sysVersion,
				userId,
				userPwd,
				isAuto,
				apiUrl,
				apiPort,
				langKey,
			} = this.$sys.getSysInfo();
			this.uid = userId
			this.upwd = userPwd
			this.isChecked = isAuto
			this.subTitle = sysVersion
			apiUrls = apiUrl
			apiPorts = apiPort
		},
		methods: {
			//更新检查
			checkUpdate: function() {
				return new Promise(function(resolve, reject) {
					let platform = uni.getSystemInfoSync().platform;
					switch (platform) {
						case 'android':
							let sys = that.$sys.getSysInfo();
							that.$http.ylget('smartPhone/getAppVersionInfo', {
								appNo: sys.appId
							}).then(function(e) {
								if (e) {
									console.log('sys',sys)
									let currentVerson = parseInt(sys.sysVersion.replace(/\./g, ''));
									let latestVerson = parseInt(e.verson.replace(/\./g, ''));
									console.log('currentVerson',currentVerson)
									console.log('latestVerson',latestVerson)
									if (latestVerson > currentVerson) {
										if (platform === 'android' && (e.androidLink == '' || e.androidLink == null)) {
											resolve(false);
											return;
										}
										if (platform === 'ios' && (e.iosLink == '' || e.iosLink == null)) {
											resolve(false);
											return;
										};
										that.$util.reLaunch('sys/update');
										resolve(true);
									}
									resolve(false);
								}
							});
							break;
						default:
							console.log('运行在开发者工具上');
							resolve(false);
							break;
					}
				})
			},
			radioClick(){
				that.isChecked = !that.isChecked
			},
			formSubmit(e) {
				var formdata = e.detail.value;
				if (that.validate(formdata)) {
					that.$util.showLoading('登录中' + '...');
					that.$http.loginPost('data/person/login', {
						fuserno: formdata.uid,
						fpassword: formdata.upwd
					}).then(e => {
						that.$util.hideLoading();
						let sysInfo = that.$sys.setUser({
							userId: e.id,
							fsiteNo: e.fsiteNo,
							name: e.username,
							isAuto: that.isChecked,
							userPwd: that.upwd,
							token: e.token,
							langKey: that.langKey,
							fsiteName: e.fsiteName
						});
						that.$util.showToast('登录成功', () => {
							that.$util.reLaunch('index/index');
						});
						
					}).catch((e) => {
						if(e == '密码不正确'){
							that.$util.showToast('密码不正确');
						}else if(e == '用户名不存在'){
							that.$util.showToast('用户名不存在');
						}else if(e == '该用户不存在'){
							that.$util.showToast('该用户不存在');
						}
						that.$util.hideLoading();
					});
				}
			},
			validate(formdata) {
				if (!that.$util.validate(formdata.uid, "账号")) return false;
				if (!that.$util.validate(formdata.upwd, "密码")) return false;
				return true;
			},
			...mapMutations(['login'])
		}
	}
</script>
<style>
	@import url("index.css");
	.radioCheck{
		margin-left: 12px;
	}
</style>
